package org.example.device.test;

import org.example.device.control.DeviceControlCommand;
import org.example.device.control.DeviceControlResult;
import org.example.device.registry.DeviceControllerRegistry;
import org.example.device.registry.DeviceProtocolRegistry;
import org.example.device.service.RealDeviceControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备控制测试控制器
 * 用于测试设备控制架构的功能
 */
@RestController
@RequestMapping("/test/device-control")
@CrossOrigin(origins = "*")
public class DeviceControlTestController {
    
    @Autowired
    private DeviceControllerRegistry controllerRegistry;
    
    @Autowired
    private DeviceProtocolRegistry protocolRegistry;
    
    @Autowired
    private RealDeviceControlService realDeviceControlService;
    
    /**
     * 测试设备控制器注册
     */
    @GetMapping("/controllers")
    public ResponseEntity<?> testControllers() {
        try {
            List<String> deviceTypes = controllerRegistry.getSupportedDeviceTypes();
            Map<String, Object> result = Map.of(
                "supportedDeviceTypes", deviceTypes,
                "controllerCount", deviceTypes.size()
            );
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", "测试设备控制器失败: " + e.getMessage()));
        }
    }
    
    /**
     * 测试设备协议注册
     */
    @GetMapping("/protocols")
    public ResponseEntity<?> testProtocols() {
        try {
            List<String> protocolTypes = protocolRegistry.getSupportedProtocolTypes();
            Map<String, Object> result = Map.of(
                "supportedProtocolTypes", protocolTypes,
                "protocolCount", protocolTypes.size()
            );
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", "测试设备协议失败: " + e.getMessage()));
        }
    }
    
    /**
     * 测试设备控制命令
     */
    @PostMapping("/{deviceId}/test-command")
    public ResponseEntity<?> testDeviceCommand(@PathVariable Long deviceId, @RequestBody Map<String, Object> request) {
        try {
            String commandType = (String) request.get("commandType");
            Object value = request.get("value");
            Long userId = Long.valueOf(request.get("userId").toString());
            
            DeviceControlCommand command = new DeviceControlCommand(deviceId, commandType, value);
            command.setUserId(userId);
            command.setAction("测试命令");
            
            DeviceControlResult result = realDeviceControlService.executeDeviceCommand(deviceId, command);
            
            if (result.isSuccess()) {
                return ResponseEntity.ok(result);
            } else {
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(DeviceControlResult.failure("测试设备命令失败: " + e.getMessage()));
        }
    }
    
    /**
     * 测试设备状态获取
     */
    @GetMapping("/{deviceId}/test-status")
    public ResponseEntity<?> testDeviceStatus(@PathVariable Long deviceId) {
        try {
            Map<String, Object> status = realDeviceControlService.getDeviceStatus(deviceId);
            return ResponseEntity.ok(status);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", "测试设备状态失败: " + e.getMessage()));
        }
    }
    
    /**
     * 测试设备连接
     */
    @GetMapping("/{deviceId}/test-connection")
    public ResponseEntity<?> testDeviceConnection(@PathVariable Long deviceId) {
        try {
            DeviceControlResult result = realDeviceControlService.testDeviceConnection(deviceId);
            
            if (result.isSuccess()) {
                return ResponseEntity.ok(result);
            } else {
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(DeviceControlResult.failure("测试设备连接失败: " + e.getMessage()));
        }
    }
    
    /**
     * 测试设备属性获取
     */
    @GetMapping("/{deviceId}/test-properties")
    public ResponseEntity<?> testDeviceProperties(@PathVariable Long deviceId) {
        try {
            Map<String, Object> properties = realDeviceControlService.getDeviceProperties(deviceId);
            return ResponseEntity.ok(properties);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", "测试设备属性失败: " + e.getMessage()));
        }
    }
    
    /**
     * 测试设备支持的指令
     */
    @GetMapping("/{deviceId}/test-commands")
    public ResponseEntity<?> testDeviceCommands(@PathVariable Long deviceId) {
        try {
            List<String> commands = realDeviceControlService.getDeviceSupportedCommands(deviceId);
            return ResponseEntity.ok(Map.of("commands", commands));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", "测试设备指令失败: " + e.getMessage()));
        }
    }
}
